package com.hod.client.lodging;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.gears.client.database.DatabaseException;
import com.gwtext.client.widgets.Window;
import com.hod.client.backend.orm.client.GearsDataObject;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.backend.orm.client.GearsQuery;
import com.hod.client.checkout.CheckOut;
import com.hod.client.reports.CheckOutDataTableView;

public class LodgingRecordset {
	
	public static boolean existsLodgingForClient(String idClient){
		GearsDataStore ds = (GearsDataStore) GWT.create(Lodging.class);
		try {
			GearsQuery q = ds.getNamedQuery(Lodging.FIND_VALID_LODGING_BY_IDCLIENT);
			q.setString(0, idClient);
			Lodging[] data = (Lodging[]) q.execute();
			return data.length>0;			
		} catch (DatabaseException e) {			
			// TODO Auto-generated catch block
			return false;
		}
		
	}
	
	public static boolean checkOut(String idClient){
		GearsDataStore ds = (GearsDataStore) GWT.create(Lodging.class);
		GearsDataStore dsCheckOut = (GearsDataStore) GWT.create(CheckOut.class);
		try {
			GearsQuery q = ds.getNamedQuery(Lodging.FIND_VALID_LODGING_BY_IDCLIENT);
			q.setString(0, idClient);
			Lodging[] data = (Lodging[]) q.execute();
			if (data.length==0)	return false;
			//hay un hospedaje valido
			//calculo el total de cargos
			/*double totalCharges = ChargeCRUD.calculateTotalCharges(data[0].getIdLodging().toString());
			
     	   //le sumo el total por los dias de ocupacion de la habitacion
			double roomCharges = RoomCRUD.calculateRoomCharge(data[0].getIdRoom());
     	   //le resto la seña (si existiese)
			double beck = ReservationCRUD.getBeck(data[0].getIdReservation());
     	   //lo guardo en total
			data[0].setTotal(totalCharges+roomCharges+beck);
			//actualizo la fecha de checkout
			data[0].setCheckOut(new Date());
     	   //cambio el estado de la habitacion a libre
			//agrego una habitacion disponible a la categoria de esa habitacion
			RoomCRUD.setRoomState(String.valueOf(data[0].getIdRoom()),"FREE");
			ds.save(data[0]);//guardo la info
			
			CheckOut checkOut = new CheckOut(); //creo una entrada en checkout
			checkOut.setIdCheckOut(null);
			checkOut.setIdClient(Integer.valueOf(idClient));
			checkOut.setIdLodging(data[0].getIdLodging());
			checkOut.setCheckOut(data[0].getCheckOut());
			checkOut.setBeck(beck);
			checkOut.setTotalCharges(totalCharges);
			checkOut.setTotalRoomCharges(roomCharges);
			dsCheckOut.save(checkOut);
			*/
			CheckOut checkOut = new CheckOut();
			checkOut.setIdClient(Integer.valueOf(idClient));
			checkOut.setIdLodging(data[0].getIdLodging());
			checkOut.setCheckOut(data[0].getCheckOut());
			Window window = new Window();
		    List<GearsDataObject> oneCheckOut = new ArrayList<GearsDataObject>();
		    oneCheckOut.add(checkOut);
			window.add((new CheckOutDataTableView()).createTable((ArrayList<GearsDataObject>) oneCheckOut));
			
			return true;
		} catch (DatabaseException e) {			
			// TODO Auto-generated catch block
			com.google.gwt.user.client.Window.alert(e.getMessage());
			return false;
		}
	}
	
	public static Lodging getLodging(Integer idLodging) {
		// TODO Auto-generated method stub
		try {
			GearsDataStore ds = (GearsDataStore) GWT.create(Lodging.class);
			Lodging o = (Lodging) ds.findById(idLodging);
			return o;
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			return null;
		} 
	}
	
	public static Lodging[] existsLodgingforRoom(Integer idRoom,Date date){
		GearsDataStore ds = (GearsDataStore) GWT.create(Lodging.class);
		try {
			GearsQuery q = ds.getNamedQuery(Lodging.EXISTS_LODGING_FOR_ROOM);
			q.setInteger(0, idRoom);
			q.setDate(1, date);
			Lodging[] data = (Lodging[]) q.execute();
			return data;			
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			return null;
		} 
		
	}
	
	public static Lodging[] existsLodgingforBed(Integer idBed,Date date){
		GearsDataStore ds = (GearsDataStore) GWT.create(Lodging.class);
		try {
			GearsQuery q = ds.getNamedQuery(Lodging.EXISTS_LODGING_FOR_BED);
			q.setInteger(0, idBed);
			q.setDate(1, date);
			Lodging[] data = (Lodging[]) q.execute();
			return data;			
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			return null;
		} 
		
	}

}
